package com.xpec.canaan.util;

import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;

/**
 * write all files output result
 * @author Wilson Shih
 *
 */
public class OutputMD5Process {
	
	public OutputMD5Process(String inputbaseDir,String outputbaseDir, TestCompareFile mainThread)
	{
		m_inputBasePath = inputbaseDir;
		m_outputBasePath = outputbaseDir;
		
		m_mainThread = mainThread;
	}
	
	public void startProcess(ExecutorService pools)
	{
		m_totalDirCount = 0;
		m_completeDirCount = 0;
		
		m_iFielProcess = pools;
		// XML部份
		String xmlPath = "//xml";
		ArrayList<File> baseXmlFiles = new ArrayList<File>();
		ArrayList<File> xmlDirs = new ArrayList<File>();
		
		// art部份
		String artPath = "//art";
		ArrayList<File> baseArtFiles = new ArrayList<File>();
		ArrayList<File> artDirs = new ArrayList<File>();
		
		// world_bg部份
		String bgPath = "//world_bg";
		ArrayList<File> baseBGFiles = new ArrayList<File>();
		ArrayList<File> bgDirs = new ArrayList<File>();
		
		ProcessSingleDirectoryRecorder.preProcess(m_inputBasePath + xmlPath, baseXmlFiles, xmlDirs);
		ProcessSingleDirectoryRecorder.preProcess(m_inputBasePath + artPath, baseArtFiles, artDirs);
		ProcessSingleDirectoryRecorder.preProcess(m_inputBasePath + bgPath, baseBGFiles, bgDirs);		
		
		m_totalDirCount = xmlDirs.size() + artDirs.size() + bgDirs.size();
		if( null != baseXmlFiles && baseXmlFiles.size()>0 )
		{
			m_totalDirCount ++;
		}
		
		createFilePath(xmlDirs,xmlPath);
		createFilePath(artDirs,artPath);
		createFilePath(bgDirs,bgPath);
		
		ProcessSingleDirectoryRecorder xmlRecord = new ProcessSingleDirectoryRecorder();
		xmlRecord.processFilesMD5(baseXmlFiles, this, m_iFielProcess , m_outputBasePath + xmlPath + "//" + CompareResultRecorder.CompareFileName);
		
		
		for( int i = 0; i < xmlDirs.size(); i++ )
		{
			ProcessSingleDirectoryRecorder subRecord = new ProcessSingleDirectoryRecorder();
			File singDir = xmlDirs.get(i);
			File[] subFiles = singDir.listFiles();
			
			subRecord.processFilesMD5(subFiles, this, m_iFielProcess, m_outputBasePath + xmlPath + "//" + singDir.getName() + "//" + CompareResultRecorder.CompareFileName);
		}
		for( int i = 0; i < artDirs.size(); i++ )
		{
			ProcessSingleDirectoryRecorder subRecord = new ProcessSingleDirectoryRecorder();
			File singDir = artDirs.get(i);
			File[] subFiles = singDir.listFiles();
			
			subRecord.processFilesMD5(subFiles, this, m_iFielProcess, m_outputBasePath + artPath + "//" + singDir.getName() + "//"+CompareResultRecorder.CompareFileName);
		}
		for( int i = 0; i < bgDirs.size(); i++ )
		{
			ProcessSingleDirectoryRecorder subRecord = new ProcessSingleDirectoryRecorder();
			File singDir = bgDirs.get(i);
			File[] subFiles = singDir.listFiles();
			
			subRecord.processFilesMD5(subFiles, this, m_iFielProcess, m_outputBasePath + bgPath + "//" + singDir.getName() + "//"+CompareResultRecorder.CompareFileName);
		}
		
	}
	
	private void createFilePath(ArrayList<File> paths, String varyPath)
	{
		for( int i = 0; i < paths.size(); i++ )
		{
			File singDir = paths.get(i);
			File mkDir = new File(m_outputBasePath + varyPath + "//" + singDir.getName() );
			mkDir.mkdirs();
		}
	}
	
	public synchronized void notifySingleDirComplete()
	{
		m_completeDirCount++;
		if( m_totalDirCount == m_completeDirCount )
		{
			System.out.println("Output Work Complete!");
			m_mainThread.notifyOutputComplete();
		}
	}
	
	private String m_inputBasePath;
	private String m_outputBasePath;
	
	private int m_totalDirCount;
	private int m_completeDirCount;
	
	private TestCompareFile m_mainThread;
	
	private ExecutorService m_iFielProcess;
}
